#include "StdAfx.h"
#include "ServerProxyMng.h"
#include "ServerProxy.h"

CServerProxyMng::CServerProxyMng(void)
{
}


CServerProxyMng::~CServerProxyMng(void)
{
}

CServerProxy* CServerProxyMng::AddServerProxy( enServerDefine kind, s_uuid uuid, xgc_lpcstr address, xgc_uint16 port, xgc_lpvoid userdata, xgc_bool connect, xgc_uint16 invent, xgc_uint16 timeout )
{
	CServerProxy* pProxy = GetServerProxy( kind );
	autolock locker( mGuard );
	if( pProxy && connect )
	{
		pProxy->Connect( uuid, address, port, userdata, invent, timeout );
	}

	return pProxy;
}

CServerProxy* CServerProxyMng::GetServerProxy( enServerDefine eServerKink )
{
	autolock locker( mGuard );
	CServerProxy *pProxy = xgc_nullptr;
	ServerMap::iterator Pos = mServerMap.find( eServerKink );
	if( Pos == mServerMap.end() )
	{
		pProxy = ServerProxyFactory::Instance().Create( eServerKink );
		mServerMap.insert(ServerMap::value_type(eServerKink,pProxy));
	}
	else
	{
		pProxy = Pos->second;
	}

	return pProxy;
}

CServerProxy* CServerProxyMng::FindServerProxy( enServerDefine eServerKink )
{
	autolock locker( mGuard );

	ServerMap::iterator Pos = mServerMap.find( eServerKink );
	if( Pos == mServerMap.end() )
	{
		return xgc_nullptr;
	}
	else
	{
		return Pos->second;
	}
}
